home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Camelot / Camelot 078 (1990-06)(Swedish User Group of Amiga)(SE)(PD)[WB].zip / Camelot 078 (1990-06)(Swedish User Group of Amiga)(SE)(PD)[WB].adf / TrackSalve / TrackSalve / Source / switch.a < prev    next >
Text File  |  1990-06-17  |  6KB  |  246 lines

  1. *
  2. *  Switch.a  -     Copyright 1990 D.W.Reisig
  3. *
  4. *   #     date     by    Comment
  5. *  -- --------- ----- ---------------------
  6. *   0 29-Oct-89     DWR   Created for Tracksalve
  7. *   1 10-Apr-90     DWR   TrackSalve 1.3, second pc catch 
  8. *
  9.  
  10. *        nolist
  11.         INCLUDE "Call.i"
  12.         INCLUDE "ts.i"
  13.         INCLUDE "exec/tasks.i"
  14.         INCLUDE "exec/resident.i"
  15. *        list
  16.  
  17.  
  18.  
  19.         CSECT    text,code,0,1,2            Any xref's after this are 16-bit reloc
  20.  
  21. *********************************************************
  22. *
  23. *  PCtoTS
  24. *
  25. *  We need to catch Trackdisk in its highest task-loop.     Otherwise we could lose control
  26. *  after an rts.  This loop is rather small:  7 instuctions.  One of the instructions is a
  27. *  jsr _LVOWait(a6). Trackdisk is waiting here for a timer or an IORequest and will be here
  28. *  most of the time.  If we change the return address to our code, Trackdisk smoothly enters
  29. *  our code.  We change this address only if TD is at this point.
  30. *
  31. *  Input:    a0    UnitData structure
  32. *        a4    Globals
  33. *
  34. *  Output:    d0    0 if caught else E_CATCHTD
  35. *
  36. *  Usage:    d2    PC of TD-task at the point where we want to change it
  37. *        d6    Timeout counter
  38. *        d7    Error
  39. *
  40. *        a2    TSControl structure
  41. *        a3    UnitData structure
  42. *        a5    Task stucture of Trackdisk unit task
  43. *
  44. *  NB!    We are accessing the TSControl structure in a way that needs protection.
  45. *
  46.  
  47.         XREF    _PCSubOffset,_PCWaitOffset
  48. WaitRtnPC    equ    -20                Offset PC to TC_SPUPPER during a Wait()
  49. SubRtnPC    equ    -16                Offset PC to TC_SPUPPER during a subcall
  50.  
  51.         Func    PCtoTS,@PCtoTS
  52.         movem.l d2-d4/d6-d7/a2-a3/a5,-(a7)
  53.  
  54.         moveq.l #0,d7                No error for now
  55.         move.l    a0,a3                UnitData structure
  56.  
  57.         move.l    UD_TDReq+IO_UNIT(a3),a5        Lea Unit structure of the Trackdisk IORequest
  58.         move.b    TDU_UnitNr(a5),d4        Get unit number (to set InUse flag later)
  59.         move.l    MP_SIGTASK(a5),a5        Lea Task control block associated with this port
  60.         move.l    UD_TSControl(a3),a2        Control structure
  61.         move.l    TSC_TDTag(a2),d2        Begin of TD code
  62.         move.l    d2,d3
  63.         add.l    #$1582,d2            Add offset for PC in subroutine
  64.         add.l    #$1592,d3            Add offset for PC when waiting
  65.         moveq.l #25,d6                We put a limit here, so we will not hang forever if TD is dead
  66.  
  67. SearchPC    ExCall    Forbid                Prevent TD to become active
  68.         move.l    TC_SPUPPER(a5),a0        Initial stackpointer of TD
  69.         cmp.l    SubRtnPC(a0),d2            Is TD in its single taskloop subroutine?
  70.         beq.s    ChangePCSub            Yes, change the return address..
  71.         cmp.l    WaitRtnPC(a0),d3        Is TD Waiting()?
  72.         beq.s    ChangePCWait            Yes, change the return address..
  73.         LibCall Permit                No, give TD a chance to change its situation
  74.         moveq.l #10,d1                We will wait for a fifth of a second
  75.         DOSCall Delay                Sleeping
  76.         dbra    d6,SearchPC            And try again..
  77.         moveq.l #E_CATCHTD,d7            But not forever, so set error
  78.         bra.s    PCtoTSRtn            Quit..
  79.  
  80. ChangePCSub    move.l    _PCSubOffset(a4),d0        PC found in the subroutine, get matching offset in TrackSalve
  81.         add.l    TSC_TSCode(a2),d0        Add begin of TrackSalve code
  82.         move.l    d0,SubRtnPC(a0)            Change return addres to adress in our code
  83.         bra.s    PCChanged
  84. ChangePCWait    move.l    _PCWaitOffset(a4),d0        PC found waiting, get matching offset in TrackSalve
  85.         add.l    TSC_TSCode(a2),d0        Add begin of TrackSalve code
  86.         move.l    d0,WaitRtnPC(a0)        Change return addres to adress in our code
  87. PCChanged
  88.         move.l    d3,UD_ReturnTD(a3)        Save TD return address (we use the Wait() call to leave TS)
  89.         move.l    TC_Userdata(a5),UD_Userdata(a3) Keep original value (although TD does not use it)
  90.         move.l    a3,TC_Userdata(a5)        Make UnitData structure known to TD-task
  91.         bset.b    d4,TSC_InUse(a2)        Set flag to prevent freeing the TSControl structure
  92.         LibCall Permit                Multitasking on again
  93.  
  94. PCtoTSRtn    move.l    d7,d0                Error
  95.         movem.l (a7)+,d2-d4/d6/d7/a2-a3/a5
  96.         rts
  97.  
  98.  
  99.  
  100. *********************************************************
  101. *
  102. *  RelocateTS
  103. *
  104. *  Modify position dependend values in copied TSCode
  105. *
  106. *  Input:    a0    TSControl structure
  107. *
  108.  
  109.         Func    RelocateTS,@RelocateTS
  110.         move.l    TSC_TSCode(a0),a0
  111.         lea.l    TSCodeRelocTable(pc),a1
  112. 10$        move.w    (a1)+,d0
  113.         beq.s    20$
  114.         move.l    a0,d1
  115.         add.l    0(a0,d0.w),d1
  116.         move.l    d1,0(a0,d0.w)
  117.         bra.s    10$
  118. 20$        rts
  119.  
  120.         xref    TSCodeRelocTable
  121.  
  122.  
  123. *********************************************************
  124. *
  125. *  RelocateTD
  126. *
  127. *  Modify position dependend values in relocated Trackdisk code versions 33.127 and 34.1
  128. *
  129. *  Input:    a0    TSControl structure
  130. *
  131.  
  132.         ifd    __stdargs
  133.         Func    _RelocateTD
  134.         move.l    4(a7),a0
  135.         endc
  136.  
  137.         Func    RelocateTD,@RelocateTD
  138.         move.l    TSC_TSTag(a0),a1        Begin of copy of Trackdisk code
  139.         move.l    TSC_TDTag(a0),d0        Begin of Trackdisk code (in ROM)
  140. *        cmp.l    a1,RT_MATCHTAG(a1)        Does our copy has its self-pointer right?
  141. *        beq.s    RelocateRtn            Yes, already relocated..
  142.         sub.l    a1,d0                TD org minus TD copy = relocation value
  143.         lea.l    RelocTable(pc),a0        Table of relative addresses to relocate
  144.         bra.s    Relocate20            Enter loop..
  145. Relocate10    sub.l    d0,0(a1,d1.w)            Only Amiga makes it possible!
  146. Relocate20    move.w    (a0)+,d1            Get offset in TD-code
  147.         bne.s    Relocate10
  148. RelocateRtn    rts
  149.  
  150.  
  151.  
  152. *--    You can obtain this table by comparing version 1.2 and 1.3 and do some editing on the result
  153.  
  154. RelocTable    dc.w    $0002
  155.         dc.w    $0006
  156.         dc.w    $000E
  157.         dc.w    $0012
  158.         dc.w    $0016
  159.         dc.w    $0324
  160.         dc.w    $032A
  161.         dc.w    $03C0
  162.         dc.w    $091C
  163.         dc.w    $0940
  164.         dc.w    $0966
  165.         dc.w    $0980
  166.         dc.w    $0994
  167.         dc.w    $09B6
  168.         dc.w    $09C4
  169.         dc.w    $09CC
  170.         dc.w    $0A00
  171.         dc.w    $0A04
  172.         dc.w    $0A08
  173.         dc.w    $0A0C
  174.         dc.w    $0A10
  175.         dc.w    $0A14
  176.         dc.w    $0A1C
  177.         dc.w    $0A20
  178.         dc.w    $0A24
  179.         dc.w    $0A28
  180.         dc.w    $0A2C
  181.         dc.w    $0A30
  182.         dc.w    $0A34
  183.         dc.w    $0A38
  184.         dc.w    $0A3C
  185.         dc.w    $0A40
  186.         dc.w    $0A44
  187.         dc.w    $0A48
  188.         dc.w    $0A4C
  189.         dc.w    $0A50
  190.         dc.w    $0A54
  191.         dc.w    $0A58
  192.         dc.w    $0A5C
  193.         dc.w    $0A60
  194.         dc.w    $0A64
  195.         dc.w    $0A68
  196.         dc.w    $0A6C
  197.         dc.w    $0A70
  198.         dc.w    $127E
  199.         dc.w    $12EA
  200.         dc.w    $1316
  201.         dc.w    $1352
  202.         dc.w    $1386
  203.         dc.w    $13E4
  204.         dc.w    $1446
  205.         dc.w    $1690
  206.         dc.w    $16B8
  207.         dc.w    $195A
  208.         dc.w    $19A2
  209.         dc.w    $19B2
  210.         dc.w    0
  211.  
  212.  
  213.  
  214. *********************************************************
  215. *
  216. *  PatchMem
  217. *
  218. *  Change memory according to a table
  219. *
  220. *  Input:    a0    Memory to change
  221. *        a1    Patchtable  (Offset.w, Size.w, Patch.w[Size])
  222. *
  223.  
  224.         Func    PatchMem,@PatchMem
  225.         movem.l a2,-(a7)
  226.         move.l    a0,a2                Lea begin of memory
  227.  
  228. GetNextPatch    move.l    a2,a0                Begin of memory
  229.         move.w    (a1)+,d0            Get offset in code to put patch
  230.         add.w    d0,a0                Lea destination of patch
  231.         move.w    (a1)+,d1            Get size of patch in words
  232.         beq.s    PatchMemRtn            No patch, done..
  233.         bra.s    CopyPatch10            Enter copyloop
  234. CopyPatch    move.w    (a1)+,(a0)+            Copy word of code
  235. CopyPatch10    dbra    d1,CopyPatch            Loop ..
  236.         bra.s    GetNextPatch            Examine next entry in Patchtable
  237.  
  238. PatchMemRtn    movem.l (a7)+,a2
  239.         rts
  240.  
  241.  
  242. *********************************************************
  243.  
  244.         END
  245.  
  246.